package com.xhcl.web.controller.system;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.xhcl.common.annotation.Log;
import com.xhcl.common.core.controller.BaseController;
import com.xhcl.common.core.domain.AjaxResult;
import com.xhcl.common.core.page.MulTableDataInfo;
import com.xhcl.common.core.page.TableDataInfo;
import com.xhcl.common.enums.BusinessType;
import com.xhcl.common.exception.BusinessException;
import com.xhcl.framework.uitls.poi.ExcelUtil;
import com.xhcl.system.domain.TGgHsdw;
import com.xhcl.system.domain.TSrhsAccountDzd;
import com.xhcl.system.domain.TSrhsAccountTemp;
import com.xhcl.system.domain.TSrhsDzd;
import com.xhcl.system.domain.TSrhsKmye;
import com.xhcl.system.service.ITGgHsdwService;
import com.xhcl.system.service.ITSrhsAccountTempService;
import com.xhcl.system.service.ITSrhsDzdService;
import com.xhcl.system.service.ITSrhsHsxtcsService;
import com.xhcl.system.service.ITSrhsZybbService;

/**
 * 单位对账单Controller
 * 
 * @author xhcl
 * @date 2020-09-03
 */
@Controller
@RequestMapping("/system/dwdzd")
public class TSrhsDwdzdController extends BaseController
{
    private String prefix = "system/dwdzd";

    @Autowired
    private ITSrhsAccountTempService tSrhsAccountTempService;
    @Autowired
    private ITSrhsHsxtcsService tSrhsHsxtcsService;
    
    @Autowired
    private ITGgHsdwService tGgHsdwService;
    
    @Autowired
    private ITSrhsZybbService tSrhsZybbService;
    
    @Autowired
    private ITSrhsDzdService tSrhsDzdService;

    @RequiresPermissions("system:dwdzd:view")
    @GetMapping()
    public String dwdzd(ModelMap mmap)
    {
    	
    	List<TSrhsDzd> dzdlist=tSrhsDzdService.selectTSrhsDzdForGrpList(null);
    	mmap.put("dzdlist", dzdlist);
        return prefix + "/dwdzd";
    }
    /**
     * 查询单位对账单列表
     * @throws ParseException 
     */
    @RequiresPermissions("system:dwdzd:list")
    @PostMapping("/list")
    @ResponseBody
    public MulTableDataInfo list(TGgHsdw tGgHsdw,ModelMap mmap) throws ParseException
    {
    	String dwbm="";
    	if (tGgHsdw.getParams().get("beginDate")==null || tGgHsdw.getParams().get("endDate")==null) {
    		throw new BusinessException("查询的日期为空");
    	}
    	List<TGgHsdw> dwlist=tGgHsdwService.selectTGgHsdwForAllList(tGgHsdw);
    	if (tGgHsdw.getParams().get("dwbm")==null) {
    		if (dwlist==null || dwlist.size()==0) {
    			throw new BusinessException("查询的起始单位未查到对应的单位对账数据");
    		}
    		dwbm=dwlist.get(0).getDwbm();
    	}else {
    		dwbm=tGgHsdw.getParams().get("dwbm").toString();
    	}
    	if (tGgHsdw.getParams().get("zbh")==null) {
    		throw new BusinessException("账本号为空");
    	}
    	int zbh=Integer.parseInt(tGgHsdw.getParams().get("zbh").toString());
    	
        Date sdate=DateUtils.parseDate(tGgHsdw.getParams().get("beginDate").toString(), new String[] {"yyyy-MM-dd"});
        Date edate=DateUtils.parseDate(tGgHsdw.getParams().get("endDate").toString(), new String[] {"yyyy-MM-dd"});
        Map<String,Object> map=new HashMap<String,Object>();
    	map.put("zbh", zbh);
    	map.put("sdate", sdate);
    	map.put("edate", edate);
    	map.put("dwbm", dwbm);
    	List<TSrhsAccountTemp> list =tSrhsAccountTempService.callDwdzd(map);
        return getMulDataTable(list,dwlist);
    }
    
    
    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected MulTableDataInfo getMulDataTable(List<?> list,List<?> sublist)
    {
    	MulTableDataInfo rspData = new MulTableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setSubrows(sublist);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
    
    /**
     * 导出单位对账单
     * @throws ParseException 
     */
    @RequiresPermissions("system:dwdzd:export")
    @Log(title = "单位对账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TGgHsdw tGgHsdw) throws ParseException
    {
    	String dwbm="";
    	if (tGgHsdw.getParams().get("beginDate")==null || tGgHsdw.getParams().get("endDate")==null) {
    		throw new BusinessException("查询的日期为空");
    	}
    	List<TGgHsdw> dwlist=tGgHsdwService.selectTGgHsdwForAllList(tGgHsdw);
    	if (tGgHsdw.getParams().get("dwbm")==null) {
    		if (dwlist==null || dwlist.size()==0) {
    			throw new BusinessException("查询的起始单位未查到对应的单位对账数据");
    		}
    		dwbm=dwlist.get(0).getDwbm();
    	}else {
    		dwbm=tGgHsdw.getParams().get("dwbm").toString();
    	}
    	if (tGgHsdw.getParams().get("zbh")==null) {
    		throw new BusinessException("账本号为空");
    	}
    	int zbh=Integer.parseInt(tGgHsdw.getParams().get("zbh").toString());
    	
        Date sdate=DateUtils.parseDate(tGgHsdw.getParams().get("beginDate").toString(), new String[] {"yyyy-MM-dd"});
        Date edate=DateUtils.parseDate(tGgHsdw.getParams().get("endDate").toString(), new String[] {"yyyy-MM-dd"});
        Map<String,Object> map=new HashMap<String,Object>();
    	map.put("zbh", zbh);
    	map.put("sdate", sdate);
    	map.put("edate", edate);
    	map.put("dwbm", dwbm);
    	List<TSrhsAccountTemp> list =tSrhsAccountTempService.callDwdzd(map);
        ExcelUtil<TSrhsAccountDzd> util = new ExcelUtil<TSrhsAccountDzd>(TSrhsAccountDzd.class);
        List<TSrhsAccountDzd> alist=new ArrayList<TSrhsAccountDzd>();
        list.forEach(m->{
        	TSrhsAccountDzd dzd=new TSrhsAccountDzd();
        	dzd.setNd(m.getNd());
        	dzd.setYf(m.getYf());
        	dzd.setRq(m.getRq());
        	dzd.setPzzh(m.getPzzh());
        	dzd.setZy(m.getZy());
        	dzd.setJjyskmmc(m.getJjyskmmc());
        	dzd.setYskm(m.getYskm());
        	dzd.setYskmmc(m.getYskmmc());
        	dzd.setJfje(m.getJfje());
        	dzd.setDfje(m.getDfje());
        	dzd.setQmye(m.getQmye());
        	alist.add(dzd);
        });
        return util.exportExcel(alist, "dwdzd");
    }

    
     
}
